package com.wframe.project.jnd.wx.controller;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.wframe.project.jnd.wx.wxImpl.WeixAccountConfig;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Administrator on 2017/7/18.
 */
@Controller
@RequestMapping("/we/features")
public class WechatFeaturesController {

    private WxMpInMemoryConfigStorage wechatMpConfig;

    private WxPayConfig wechatPayConfig;

    private WxMpService wxMpService;

    private WxPayService wxPayService;

    private static final String SIGNATURE_TOKEN_KEY = "SIGNATURE_TOKEN";

    @Autowired
    private WeixAccountConfig tempWechatConfig;

    private void initWechatConfig() {
        this.wechatMpConfig = new WxMpInMemoryConfigStorage();
        this.wechatMpConfig.setAppId(this.tempWechatConfig.getAppId());
        this.wechatMpConfig.setSecret(this.tempWechatConfig.getSecret());
        this.wechatMpConfig.setToken(this.tempWechatConfig.getToken());
        this.wxMpService = new WxMpServiceImpl();
        this.wxMpService.setWxMpConfigStorage(this.wechatMpConfig);

        this.wechatPayConfig = new WxPayConfig();
        this.wechatPayConfig.setAppId(this.tempWechatConfig.getAppId());
        this.wechatPayConfig.setMchId(this.tempWechatConfig.getMch_id());
        this.wechatPayConfig.setMchKey(this.tempWechatConfig.getPayKey());
        this.wechatPayConfig.setTradeType("JSAPI");
        this.wxPayService = new WxPayServiceImpl();
        this.wxPayService.setConfig(this.wechatPayConfig);
    }

    @RequestMapping("/scanBuy")
    public String showScanBuy(HttpSession session) {
        session.setAttribute(SIGNATURE_TOKEN_KEY, UUID.randomUUID().toString());
        return "/wesite/view/buyplan/scan-results";
    }

    @RequestMapping("/signature")
    @ResponseBody
    public Map<String, Object> generateSignature(String token, String url, HttpSession session) {
        if (StringUtils.isEmpty(token) || StringUtils.isEmpty(url)) {
            return null;
        }
        if (session.getAttribute(SIGNATURE_TOKEN_KEY) == null || (!token.equals(session.getAttribute(SIGNATURE_TOKEN_KEY)))) {
            return null;
        }
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            initWechatConfig();
            WxJsapiSignature jsapiSignature = this.wxMpService.createJsapiSignature(url);
            result.put("result", "yes");
            result.put("jsapi", jsapiSignature);
        } catch (WxErrorException e) {
            e.printStackTrace();
            result.put("result", "no");
            result.put("message", e.getMessage());
        }
        return result;
    }

}
